<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="false"
    :showHeader="false"
    :okAuth="'puorder:poorder:comfirm'"
    @register="registerDrawer2"
    @ok="handleComfirm"
    width="80%"
  >
  <div>
    <div>
      <BasicTable @register="registerTable" @row-click="handleRowClick">
        <template #tableTitle>
          <Icon :icon="getTitle.icon" class="m-1 pr-1" />
          <span> {{ getTitle.value }} </span>
        </template>
        <template #toolbar> </template>
        <template #firstColumn="{ record }">
          <a @click="handleForm({ id: record.id })">
            {{ record.unitname }}
          </a>
        </template>
      </BasicTable>
    </div>
    <div>
      <BasicTable @register="registerTable2">
        <template #firstColumn="{ record }">
          <a @click="handleForm({ id: record.id })">
            {{ record.unitname }}
          </a>
        </template>
      </BasicTable>
    </div>
  </div>
</BasicDrawer>
</template>
<script lang="ts" setup name="ViewsSrmPuorderOrderPoorderListOrderChg">
  import { unref, onMounted } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import {
    poorderDelete,
    poorderListData,
    poorderBListData,
    chgHistoryListData,
  } from '/@/api/srm/puorder/order/poorder';
  import { useDrawer,useDrawerInner, BasicDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';

  const { t } = useI18n('puorder.poorder');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value:  t('订单变更记录'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    showResetButton:false,
    schemas: [
      // {
      //   label: t('公司名称'),
      //   field: 'unitname',
      //   component: 'Input',
      // },
      {
        label: t('订单编号'),
        field: 'vordercode',
        component: 'Input',
        componentProps: {
          disabled: true,
        },
      },
      {
        label: t('版本号'),
        field: 'nversion',
        component: 'Input',
        componentProps: {
          allowClear: true,
        },
      },
      // {
      //   label: t('版本号'),
      //   field: 'orderBy',
      //   component: 'Input',
      //   defaultValue:'a.nversion desc',
      //   show:false
      // },
      // {
      //   label: t('订单日期'),
      //   field: 'dorderdate',
      //   component: 'DatePicker',
      //   componentProps: {
      //     format: 'YYYY-MM-DD',
      //     showTime: false,
      //   },
      // },
      // {
      //   label: t('业务流程'),
      //   field: 'businame',
      //   component: 'Input',
      // },
      // {
      //   label: t('采购组织'),
      //   field: 'puname',
      //   component: 'Input',
      // },
      // {
      //   label: t('供应商'),
      //   field: 'custName',
      //   component: 'Input',
      // },
      // {
      //   label: t('采购员'),
      //   field: 'psnName',
      //   component: 'Input',
      // },
      // {
      //   label: t('采购部门'),
      //   field: 'deptName',
      //   component: 'Input',
      // },
      // {
      //   label: t('制单人'),
      //   field: 'createByName',
      //   component: 'Input',
      // },
      // {
      //   label: t('制单时间'),
      //   field: 'createDate',
      //   component: 'DatePicker',
      //   componentProps: {
      //     format: 'YYYY-MM-DD',
      //     showTime: false,
      //   },
      // },
      // {
      //   label: t('修改人'),
      //   field: 'updateByName',
      //   component: 'Input',
      // },
      // {
      //   label: t('修改时间'),
      //   field: 'updateDate',
      //   component: 'DatePicker',
      //   componentProps: {
      //     format: 'YYYY-MM-DD',
      //     showTime: false,
      //   },
      // },
      // {
      //   label: t('审核人'),
      //   field: 'auditByName',
      //   component: 'Input',
      // },
      // {
      //   label: t('审核时间'),
      //   field: 'auditDate',
      //   component: 'DatePicker',
      //   componentProps: {
      //     format: 'YYYY-MM-DD',
      //     showTime: false,
      //   },
      // },
      // {
      //   label: t('订单状态'),
      //   field: 'orderStatus',
      //   component: 'Select',
      //   componentProps: {
      //     dictType: 'xy_po_order_status',
      //     allowClear: true,
      //     onChange: handleSuccess,
      //   },
      // },
      // {
      //   label: t('是否最新版本'),
      //   field: 'bislatest',
      //   component: 'Input',
      // },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('订单编号'),
      dataIndex: 'vordercode',
      key: 'a.vordercode',
      sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('版本号'),
      dataIndex: 'versionDesc',
      key: 'a.nversion',
      sorter: true,
      width: 80,
      align: 'center',
      customCell: (record: Recordable) => {
        const color = record.bislatest == 'Y' ? 'red' : '';
        const size = record.bislatest == 'Y' ? 'large' : '';
        return {
          // innerHTML: record.testTextarea, // 原样输出，不进行html编码（例子）
          // style: `background-color: ${color} !important`,
          style: `color: ${color} !important;font-size: ${size};`,
        };
      },
    },
    {
      title: t('订单日期'),
      dataIndex: 'dorderdate',
      key: 'a.dorderdate',
      sorter: true,
      width: 120,
      align: 'center',
    },
    {
      title: t('修订日期'),
      dataIndex: 'updateDate',
      key: 'a.update_date',
      sorter: true,
      width: 120,
      align: 'center',
    },
    {
      title: t('公司'),
      dataIndex: 'unitname',
      key: 'a.unitname',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('业务流程'),
      dataIndex: 'businame',
      key: 'a.businame',
      sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('采购组织'),
      dataIndex: 'puname',
      key: 'a.puname',
      sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('供应商'),
      dataIndex: 'custName',
      key: 'a.cust_name',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('采购员'),
      dataIndex: 'psnName',
      key: 'a.psn_name',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('采购部门'),
      dataIndex: 'deptName',
      key: 'a.dept_name',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('备注信息'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('制单人'),
      dataIndex: 'createByName',
      key: 'a.create_by_name',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('审批人'),
      dataIndex: 'auditByName',
      key: 'a.audit_by_name',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('审批日期'),
      dataIndex: 'auditDate',
      key: 'a.audit_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
  ];

  const table2Columns: BasicColumn[] = [
    {
      title: t('行号'),
      dataIndex: 'crowno',
      key: 'a.crowno',
      sorter: true,
      width: 80,
      align: 'center',
    },
    {
      title: t('存货编码'),
      dataIndex: 'cinvcode',
      key: 'a.cinvcode',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('存货名称'),
      dataIndex: 'cinvname',
      key: 'a.cinvname',
      sorter: true,
      width: 200,
      align: 'left',
    },
    {
      title: t('规格'),
      dataIndex: 'cinvspec',
      key: 'a.cinvspec',
      sorter: true,
      width: 120,
      align: 'left',
    },
    {
      title: t('型号'),
      dataIndex: 'cinvtype',
      key: 'a.cinvtype',
      sorter: true,
      width: 120,
      align: 'left',
    },
    {
      title: t('计量单位'),
      dataIndex: 'measname',
      key: 'a.measname',
      sorter: true,
      width: 80,
      align: 'center',
    },
    {
      title: t('数量'),
      dataIndex: 'nordernum',
      key: 'a.nordernum',
      sorter: true,
      width: 80,
      align: 'right',
    },
    {
      title: t('计划到货日期'),
      dataIndex: 'dplanarrvdate',
      key: 'a.dplanarrvdate',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('激活状态'),
      dataIndex: 'bactive',
      key: 'a.bactive',
      dictType: 'srm_order_row_active_status',
      sorter: true,
      width: 80,
      align: 'center',
    },
    {
      title: t('关闭人'),
      dataIndex: 'closeByName',
      width: 80,
      align: 'left',
    },
    {
      title: t('关闭日期'),
      dataIndex: 'closeDate',
      width: 90,
      align: 'left',
    },
    {
      title: t('关闭/打开理由'),
      dataIndex: 'closingreason',
      width: 120,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'i-clarity:note-edit-line',
        title: t('编辑采购订单'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'puorder:poorder:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload,getForm }] = useTable({
    api: chgHistoryListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    //actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    // scroll: {
    //   y: 250,
    // },
    maxHeight: 250,
    rowSelection: {
      type: 'radio',
    },
    immediate: false,
    defSort:{
      orderBy:'a.nversion desc'
    }
  });

  const [registerTable2, { reload: reload2 ,setTableData}] = useTable({
    api: poorderBListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: table2Columns,
    //actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: false,
    useSearchForm: false,
    canResize: true, //表格是否flex布局
    maxHeight: 400,
    immediate: false, //懒加载
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  // 鼠标行点击事件，获取上表数据
  function handleRowClick(record: Recordable) {
    console.log(record);
    record.onEdit?.(true, false);
    reload2({
      searchInfo: { parentId: record.id },
    });
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }

  function handleSuccess2(record: Recordable) {
    reload2({ record });
  }


  // onMounted(async () => {
  //   handleBg();
  // });
  // async function handleBg() {
  //   await getForm().setFieldsValue({
  //     vordercode: router.currentRoute.value.query.vordercode,
  //   });
  //   reload();
  //   await getForm().setFieldsValue({
  //     vordercode: router.currentRoute.value.query.vordercode,
  //   });
  // }


  const [registerDrawer2, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await getForm().setFieldsValue({
      vordercode: data.vordercode,
    });
    reload();
    await getForm().setFieldsValue({
      vordercode: data.vordercode,
    });
    setTableData([])
    setDrawerProps({ loading: false });
  });
</script>
